#encoding=UTF-8
'''
Created on 2012-8-13

@author: Administrator
'''
from config.settings import my_config
from django.template.context import Context
from django.template.loader import get_template
from tool import temp_map

def getTop():
    t=get_template(temp_map["top"]);
    return t.render(Context(my_config));

'''
常用html元素配置 及常用字符变量
'''
htmlElem=',div,span,ul,li,dd,dt,'

_nvlStr=''
_sub='sub'
_render='render'
_map='map'
_module='module'
_start='<'
_end='>'
_endflag='</'
_dou=","

class renderObj:
    def __init__(self,data=None,render=None):
        '''
        
        @param data: 配置数据
        @param render: 
        '''
        self.result=''
        self.data = data
        self.render = render
    def getModule(self):
        for mod in self.data[_module]:
            elem=mod[_render];
            if(htmlElem.find(_dou+elem+_dou)>-1):
                self.result+=self.addStartFlag(elem)
                self.renderSub(mod[_sub])
                self.result+=self.addEndFlag(elem)
            else:
                temp=get_template(temp_map[mod[_map]])
                data=self.render[elem]
                self.result+=temp.render(Context(data))

        # 返回结果       
        return self.result
            
            
    def addStartFlag(self,elem):
        return _start+elem+_end
    
    def addEndFlag(self,elem):
        return _endflag+elem+_end

    def renderSub(self,data):
        if(data!=None):
            for mod in data:
                elem=mod[_render]
                if(htmlElem.find(_dou+elem+_dou)>-1):
                    self.result+=self.addStartFlag(elem)
                    self.renderSub(mod[_sub])
                    self.result+=self.addEndFlag(elem)
                else:
                    temp=get_template(temp_map[mod[_map]])
                    data=self.render[elem]
                    self.result+=temp.render(Context(data))
            return self.result
        else:
            return _nvlStr
                    